package com.javamsg.ui;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Monitor;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;

import com.javamsg.beans.FileBean;

import static com.javamsg.CommandDefinition.IPMSG_FILE_REGULAR;

public class FileTransferDialog {
	private Display display;
	private Shell shell;
	
	private Table table;		// use for represent file transfer processing
	
	private FileBean[] fileList;
	
	private long finishSize = 0;
	
	private static final String TITLE = "File Transfer Manager";
	private static final String[] columnNames = {"NO.",
												"name",
												"size",
												"type",
												"progress",
												"finished",
												"speed",
												};
	
	public FileTransferDialog(FileBean[] _FileList){
		this.fileList = _FileList;
		
	}
	
	public Shell open(Display _display){
		shell = new Shell(_display);
		shell.setText(TITLE);
		GridLayout layout = new GridLayout();
		layout.numColumns = 1;
		shell.setLayout(layout);
		createTopPart();
		createBottomPart();
		
		shell.setSize(table.computeSize(SWT.DEFAULT, SWT.DEFAULT).x, 300);
		moveWindowToCenter();
		shell.open();
		return shell;
	}
	/**
	 * 
	 */
	public void fillTable(){
		if(fileList == null){
			return;
		}
		for(int i = 0; i < fileList.length; i++){
			TableItem item = new TableItem(table, SWT.NONE);
			FileBean fileAttr = fileList[i];
			item.setText(0, fileAttr.getFileNo() + "");		// number
			item.setText(1, fileAttr.getFileName());		// file name
			item.setText(2, fileAttr.getFileSize()/1024 + "kb");	// file size
			// file type
			item.setText(3, (fileAttr.getFileAttr() == IPMSG_FILE_REGULAR ? "file" : "directory"));
			item.setText(4, "0%");			// progress
			item.setText(5, "0kb");			// finish size
			item.setText(6, "0kbps");		// speed
		}
	}
	/**
	 * update transfer progress
	 * @param fileNo
	 * @param finishSize
	 * @param speed
	 */
	public void updateProgress(String fileNo, int transferSize, int seconds){
		
		int fileNumber = Integer.parseInt(fileNo);
		finishSize = finishSize + transferSize;
		String progress = finishSize / (fileList[fileNumber].getFileSize()) + "%";
		String displayFinishSize = finishSize / 1024 + "KB";			
		if(finishSize / 1024 >= 1024){
			displayFinishSize = finishSize / 1024 / 1024 + "MB";
		}
		String displaySpeed = transferSize / seconds / 1024 + "kbps";
		if(transferSize / seconds / 1024 >= 1024){
			displaySpeed = transferSize / seconds / 1024 / 1024 + "mbps";
		}
		
		TableItem item = table.getItem(fileNumber);
		item.setText(4, progress);
		item.setText(5, displayFinishSize);
		item.setText(6, displaySpeed);
		
	}
	
	private void createTopPart(){
		table = new Table(shell, SWT.BORDER | SWT.FULL_SELECTION | SWT.SINGLE);
		
		GridData gridData = new GridData();
		gridData.horizontalAlignment = SWT.FILL;
		gridData.grabExcessHorizontalSpace = true;
		gridData.verticalAlignment = SWT.FILL;
		gridData.grabExcessVerticalSpace = true;
		table.setLayoutData(gridData);
		table.setHeaderVisible(true);
		table.setLinesVisible(true);
		
		// number column
		TableColumn numberColumn = new TableColumn(table, SWT.NONE);
		numberColumn.setText(columnNames[0]);
		numberColumn.setWidth(50);
		// file name
		TableColumn fileNameColumn = new TableColumn(table, SWT.NONE);
		fileNameColumn.setText(columnNames[1]);
		fileNameColumn.setWidth(200);
		// file size
		TableColumn fileSizeColumn = new TableColumn(table, SWT.NONE);
		fileSizeColumn.setText(columnNames[2]);
		fileSizeColumn.setWidth(50);
		// file type
		TableColumn fileTypeColumn = new TableColumn(table, SWT.NONE);
		fileTypeColumn.setText(columnNames[3]);
		fileTypeColumn.setWidth(50);
		// progress
		TableColumn progressColumn = new TableColumn(table, SWT.NONE);
		progressColumn.setText(columnNames[4]);
		progressColumn.setWidth(100);
		
		// finish size
		TableColumn finishSizeColumn = new TableColumn(table, SWT.NONE);
		finishSizeColumn.setText(columnNames[5]);
		finishSizeColumn.setWidth(55);
		
		// speed
		TableColumn speedColumn = new TableColumn(table, SWT.NONE);
		speedColumn.setText(columnNames[6]);
		speedColumn.setWidth(50);
		
	}
	private void createBottomPart(){
		Composite composite = new Composite(shell, SWT.NONE);
		
		GridData gridData = new GridData();
		gridData.horizontalAlignment = SWT.FILL;
		gridData.grabExcessHorizontalSpace = true;
		gridData.verticalAlignment = SWT.BOTTOM;
		gridData.grabExcessVerticalSpace = false;
		composite.setLayoutData(gridData);
		
		GridLayout layout = new GridLayout();
		layout.numColumns = 2;
		composite.setLayout(layout);
		
		Button deleteAllButton = new Button(composite, SWT.PUSH);
		deleteAllButton.setText("Delete All");
		Button closeButton = new Button(composite, SWT.PUSH);
		closeButton.setText("Close");
		
	}
	
	/**
	 * move the window to the center of the monitor
	 */
	private void moveWindowToCenter(){
		Monitor monitor = this.shell.getMonitor();
		Rectangle bounds = monitor.getBounds();
		Rectangle rect = shell.getBounds();         
	    int x = bounds.x + (bounds.width - rect.width) / 2;          
	    int y = bounds.y + (bounds.height - rect.height) / 2;          
	    shell.setLocation (x, y);          
	}
	
	public static void main(String[] args){
		int testSize = 5;
		FileBean[] files = new FileBean[testSize];
		for(int i = 0; i < testSize; i++){
			FileBean bean = new FileBean();
			bean.setFileNo(i);
			bean.setFileName("fileName");
			bean.setFileAttr(IPMSG_FILE_REGULAR);
			bean.setFileSize(1024*8);
			files[i] = bean;
		}
		FileTransferDialog dialog = new FileTransferDialog(files);
		Display display = new Display();
		Shell shell = dialog.open(display);
		dialog.fillTable();
		dialog.updateProgress("3", 1024, 1);
		dialog.updateProgress("3", 1024*1024*9, 1);
		while(!shell.isDisposed()){
			if(!display.readAndDispatch())
				display.sleep();
		}
		display.dispose();
	}
}
